<?php

namespace app\yj\controller;

use app\yj\model\MenuModel;
use app\yj\model\QuyuModel;
use app\yj\model\RoleModel;
use app\yj\model\RuleModel;
use app\yj\model\UserModel;
use think\Controller;
use think\Request;

class LoginController extends Controller
{

    //登录接口

    private static $aes_key = '12aasa21dakqnbzv';

    public function login(Request $request)
    {
        $data = $request->post();
        $username = array_key_exists('username', $data) ? $data['username'] : '';
        $password = array_key_exists('password', $data) ? $data['password'] : '';


        if ($username == '' || $password == '') return json(['code' => 400, 'msg' => '缺少必要参数']);

        //验证是否正在使用

        $user = UserModel::where('username', $username)
            ->where('password', $password)
            ->find();


        if ($user) {
            $user['rules'] = [];

            if ($user['roles'] != '' || $user['roles'] == 0) {

                if ((int)$user['roles'] == 0) {
                    //获取全部权限
                    $user['rules'] = RuleModel::select();

                } else {

                    $aa = [];
                    $roles = RoleModel::where('id', $user['roles'])->find();
                    foreach ($roles['rules'] as $v) {
                        $rule = RuleModel::find($v);
                        array_push($aa, $rule);
                    }
                    $user['rules'] = $aa;
                }
            }
//
            $menus = $this->cl_rulemenu($user['rules']);
            unset($user['rules']);
            $user['menus'] = $menus;
            if ($user['qyid'] != '') {
                $user['quobj'] = QuyuModel::find($user['qyid']);
            }

            return json(['code' => 200, 'msg' => '登录成功', 'token' => $this->encrypt($user), 'user' => $user]);
        } else {
            return json(['code' => 400, 'msg' => '用户名或密码错误']);
        }
    }

    public function cl_rulemenu($rules = [])
    {
        $menurules = [];
        $menuids = [];
        foreach ($rules as $k => $v) {
            //先判断是否存在父级目录 没有则访问 创建
            if (!in_array($v['mid'], $menuids)) {

                //访问数据库
                $menus = MenuModel::find($v['mid']);

                if ($menus) {
                    $menus['children'] = [
                        $v
                    ];
                    array_push($menurules, $menus);
                    array_push($menuids, $v['mid']);
                }
            } else {
                foreach ($menurules as $k => $r) {

                    if ($r['id'] == $v['mid']) {
                       
                        $aa = $menurules[$k]['children'];
                        array_push($aa, $v);
                        $menurules[$k]['children'] = $aa;
                    }

                }
            }
        }
        return $menurules;
    }

    /**
     * 加密
     * @param string $str 要加密的数据
     * @return bool|string   加密后的数据
     */
    public static function encrypt($str)
    {

//        $data = openssl_encrypt(json_encode($str), 'AES-128-ECB', self::$aes_key, OPENSSL_RAW_DATA);
        $data = base64_encode($str);

        return $data;
    }


}